package com.wxzz.elearing.course.quartz;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.wxzz.elearing.course.dto.StudyTimeDto;
import com.wxzz.elearing.course.entity.WxUserCurriculumData;
import com.wxzz.elearing.course.mapper.WxUserCurriculumDataMapper;
import com.wxzz.elearing.course.service.openfeign.IEnterpriseFeign;
import com.wxzz.elearning.common.aop.Idempotent;
import com.wxzz.elearning.common.constants.RedisConstants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Component
public class WxUserCurriculumDataQuartz {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private WxUserCurriculumDataMapper wxUserCurriculumDataMapper;

    @Scheduled(cron = "0 55 23 * * ?")
    public void studyPlanFinish() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.CURRICULUM_DATA_QUARTZ, RedisConstants.CURRICULUM_DATA_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
        List<WxUserCurriculumData> wxUserCurriculumData = wxUserCurriculumDataMapper.selectList(new QueryWrapper<>());
        for (WxUserCurriculumData wxUserCurriculumDatum : wxUserCurriculumData) {
            //每日清零
            wxUserCurriculumDatum.setTodayStudyTime(0L);
            wxUserCurriculumDataMapper.updateById(wxUserCurriculumDatum);
        }
    }

    @Autowired
    private IEnterpriseFeign enterpriseFeign;

    /**
     * 企业人均学习时长统计
     */
    @Scheduled(cron = "0 50 23 * * ?")
    //@Scheduled(cron = "0/2 * * * * ?")
    public void studyTime() {
        //分布式环境下进行幂等
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(RedisConstants.CURRICULUM_STUDY_DATA_QUARTZ, RedisConstants.CURRICULUM_STUDY_DATA_QUARTZ, 60 * 60, TimeUnit.SECONDS);
        if (!Boolean.TRUE.equals(aBoolean)) {
            //如果已存在则直接退出任务
            return;
        }
        //查询所有企业
        List<Long> longs = enterpriseFeign.selectAllEnterprise();
        for (Long aLong : longs) {
            String studyList = redisTemplate.opsForValue().get(RedisConstants.CURRICULUM_STUDY_DATA + aLong);
            List<StudyTimeDto> maps = Lists.newArrayList();
            if (StringUtils.isNotBlank(studyList)) {
                maps = JSONObject.parseArray(studyList, StudyTimeDto.class);
            }
            //查询企业下所有人的学习时长
            List<WxUserCurriculumData> wxUserCurriculumDatas = wxUserCurriculumDataMapper.selectList(new LambdaQueryWrapper<WxUserCurriculumData>()
                    .eq(WxUserCurriculumData::getEnterpriseId, aLong));
            //计算人均学习时长
            double studyTime = 0;
            //学习总时长
            Long allStudyTime = 0L;
            for (WxUserCurriculumData wxUserCurriculumData : wxUserCurriculumDatas) {
                allStudyTime = allStudyTime + wxUserCurriculumData.getTodayStudyTime();
            }
            if (wxUserCurriculumDatas.size() == 0) {
                studyTime = 0;
            } else {
                studyTime = (double) allStudyTime / wxUserCurriculumDatas.size();
            }
            StudyTimeDto studyTimeDto = new StudyTimeDto();
            studyTimeDto.setLocalDate(LocalDate.now().getMonth().getValue()+"-"+LocalDate.now().getDayOfMonth());
            studyTimeDto.setStudyTime(studyTime);
            maps.add(studyTimeDto);
            //截取7天
            if (maps.size() >= 7) {
                List<StudyTimeDto> subList = maps.subList(maps.size() - 7, maps.size());
                redisTemplate.opsForValue().set(RedisConstants.CURRICULUM_STUDY_DATA + aLong, JSONObject.toJSONString(subList));
            } else {
                redisTemplate.opsForValue().set(RedisConstants.CURRICULUM_STUDY_DATA + aLong, JSONObject.toJSONString(maps));
            }
        }


    }

//    public static void main(String[] args) {
//        List list = new ArrayList();
//        list.add("1");
//        list.add("2");
//        list.add("3");
//        list.add("4");
//        list.add("5");
//        List list1 = list.subList(list.size() - 3, list.size());
//        System.out.println(list1);
//    }

}
